package com.cqeec.myblog.service;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.cqeec.myblog.dao.ArticleDao;
import com.cqeec.myblog.dao.ArticleDaoImpl;
import com.cqeec.myblog.dao.DataDictionaryDao;
import com.cqeec.myblog.dao.DataDictionaryDaoImpl;
import com.cqeec.myblog.model.Article;
import com.cqeec.myblog.model.DataDictionary;

/**
 * 文章服务实现类(业务逻辑处理类)。
 * 
 * @author TLF。
 *
 */
public class ArticleServiceImpl implements ArticleService {

	// 创建文章数据访问对象赋给文章接口变量
	private ArticleDao articleDao = new ArticleDaoImpl();

	private DataDictionaryDao ddDao = new DataDictionaryDaoImpl();

	@Override
	public void delArticleType(String id) throws ClassNotFoundException, SQLException {
		ddDao.delete(id);
	}

	@Override
	public void addArticleType(DataDictionary dd) throws ClassNotFoundException, SQLException {
		ddDao.insert(dd);
	}

	@Override
	public List<Article> getAllArticle() {
		// 调用方法获取所有文章对象列表
		List<Article> list = articleDao.select();
//		if (list != null) {
//			// 遍历所有文章对象
//			String content;
//			Random r = new Random();
//			for (Article a : list) {
//				// 将文章内容随机的取出前部分+......
//				content = a.getContent();
//				content = content.substring(0, r.nextInt(content.length())) + "......";
//				a.setContent(content);
//			}
//		}
		return list;
	}

	@Override
	public List<DataDictionary> getArticleType() throws ClassNotFoundException, SQLException {
		return ddDao.selectByType("ArticleType");
	}
	
	@Override
	public Map<String,Integer> getArticleTypeAndTotal() throws ClassNotFoundException, SQLException {
		return ddDao.selectBlogerArticleTypeAndTotal();
	}

	@Override
	public void saveArticle(Article article) throws ClassNotFoundException, SQLException {
		// 生成ID
		article.setId(UUID.randomUUID().toString().toUpperCase());
		// 设置创建时间
		article.setCreationTime(new Date());
		// 设置是否发布标识
		article.setIsReleased(false);
		articleDao.insert(article);
	}

	@Override
	public List<Article> getArticleByType(String type) throws ClassNotFoundException, SQLException {
		return articleDao.findByType(type);
	}

	@Override
	public List<Article> getArticleByPage(int pageSize, int pageNumber) throws ClassNotFoundException, SQLException {
		return articleDao.findByPage(pageSize, pageNumber);
	}

	@Override
	public int getRecordsTotalNumber() throws ClassNotFoundException, SQLException {
		return articleDao.count();
	}

	@Override
	public int getRecordsTotalNumber(String type) throws ClassNotFoundException, SQLException {
		return articleDao.count(type);
	}

	@Override
	public List<Article> getArticleByPage(int pageSize, int pageNumber, String type)
			throws ClassNotFoundException, SQLException {
		return articleDao.findByPage(pageSize, pageNumber, type);
	}

	@Override
	public Article getArticle(String id) throws ClassNotFoundException, SQLException {
		return articleDao.findById(id);
	}

}
